﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Linq;
using Microsoft.Win32;
using System.IO;


//this is necessary to detect the location of Visual Studio
//some installations don't have a registry key to point to it
namespace PluginInstallHelper
{
    [RunInstaller(true)]
    public partial class Installer1 : System.Configuration.Install.Installer
    {
        //installContext //installContext;

        private readonly string[] SOURCE_FILES = { "extension.vsixmanifest", "ispPlugin.dll", "ispPlugin.pkgdef", "ispPlugin.vsix" };
        private readonly string[] DEST_FILES = { "extension.vsixmanifest", "ispPlugin.dll", "ispPlugin.pkgdef", "ispPlugin.vsix" };
        public Installer1()
        {
            InitializeComponent();
        }
        private string GetVSInstallPath()
        {
            RegistryKey visStudio = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\VisualStudio\10.0");
            string visStudioPath;
            if (visStudio == null)
            {
                visStudio = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\VisualStudio\10.0_Config");
                //try to find it on system
                visStudioPath = @"C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\";
                if (!File.Exists(visStudioPath))
                {
                    visStudioPath = @"C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\";
                    if (!File.Exists(visStudioPath))
                    {
                        return string.Empty;
                    }
                }
            }
            else
            {
                visStudioPath = visStudio.GetValue("InstallDir") as string;
                if (visStudioPath == null)
                {
                    return string.Empty;
                }
            }
            return visStudioPath;
        }

        public override void Install(IDictionary stateSaver)
        {
            //installContext = new //installContext(
                      //Path.Combine(this.Context.Parameters["InstallPath"], "PluginInstall.log"), null);
            
            base.Install(stateSaver);

            stateSaver.Remove("Installed Plugin");

            //from CustomActionData
            string installPath = this.Context.Parameters["InstallPath"];

            string visStudioPath = GetVSInstallPath();
            if (visStudioPath == string.Empty)
            {
                //installContext.LogMessage("Could not find Visual Studio 2010 installation");
                return;
            }
            if (!File.Exists(visStudioPath + @"Extensions"))
            {
                try
                {
                    Directory.CreateDirectory(visStudioPath + @"Extensions");
                }
                catch (Exception e)
                {
                    //installContext.LogMessage("Unable to create [VS Path]\\Extensions directory:" + e.Message);
                    return;
                }
            }
            if (!File.Exists(visStudioPath + @"Extensions\GaussGroup\ISP\0.3.0"))
            {
                try
                {
                Directory.CreateDirectory(visStudioPath + @"Extensions\GaussGroup\ISP\0.3.0");
                }
                catch(Exception e)
                {
                    //installContext.LogMessage("Unable to create [VS Path]\\Extensions\\GaussGroup\\ISP\\0.3.0 directory: " +
                        //e.Message);
                    return;
                }
            }
            string dest = visStudioPath + @"Extensions\GaussGroup\ISP\0.3.0";
            if(Directory.GetFiles(dest).Count() > 0){
                foreach (string file in Directory.GetFiles(dest))
                {
                    try
                    {
                        File.Delete(file);
                    }
                    catch (Exception e)
                    {
                        //installContext.LogMessage("Couldn't remove old plug-in files: " + e.Message);
                        return;
                    }
                }
            }
            for(int x = 0; x < SOURCE_FILES.Length; ++x){
                string file = SOURCE_FILES[x];
                string destFile = DEST_FILES[x];
                try
                {
                    File.Copy(Path.Combine(installPath, file), Path.Combine(dest, destFile));
                }
                catch (Exception e)
                {
                    //installContext.LogMessage("Couldn't copy new plug-in files: " + e.Message);
                    return;
                }
            }
            stateSaver.Add("Installed Plugin", true);
        }
        public override void Uninstall(IDictionary savedState)
        {
            //installContext = new //installContext(
                      //Path.Combine(this.Context.Parameters["InstallPath"], "PluginInstall.log"), null);

            bool errors = false; 

            base.Uninstall(savedState);

            if (!savedState.Contains("Installed Plugin"))
            {
                return;
            }
            //System.Windows.Forms.MessageBox.Show("We're breaking in Plugin Uninstall now . . .");
            //StreamWriter log = new StreamWriter(@"c:\users\sawaya\desktop\uninstallLog.txt");

            string vsPath = GetVSInstallPath();
            if (vsPath == string.Empty)
            {
                //installContext.LogMessage("Could not find Visual Studio 2010 installation");
                return;
            }
            try
            {
                Directory.Delete(Path.Combine(vsPath, @"Extensions\GaussGroup\ISP\0.3.0"), true);
            }
            catch (Exception e)
            {
                //installContext.LogMessage("Couldn't remove plug-in file: " + e.Message);
                errors = true;
            }
            if (Directory.EnumerateFiles(Path.Combine(vsPath, @"Extensions\GaussGroup\ISP")).Count() == 0 &&
                Directory.EnumerateDirectories(Path.Combine(vsPath, @"Extensions\GaussGroup\ISP")).Count() == 0)
            {
                try
                {
                    Directory.Delete(Path.Combine(vsPath, @"Extensions\GaussGroup\ISP"));
                }
                catch (Exception e)
                {
                    //installContext.LogMessage("Couldn't remove plug-in directory: " + e.Message);
                    errors = true;
                }
            }

            if (Directory.EnumerateFiles(Path.Combine(vsPath, @"Extensions\GaussGroup")).Count() == 0 &&
                Directory.EnumerateDirectories(Path.Combine(vsPath, @"Extensions\GaussGroup")).Count() == 0)
            {
                try
                {
                    Directory.Delete(Path.Combine(vsPath, @"Extensions\GaussGroup"));
                }
                catch (Exception e)
                {
                    //installContext.LogMessage("Unable to remove extension path: " + e.Message);
                    errors = true;
                }
            }
            if (!errors)
            {
                savedState.Remove("Installed Plugin");
            }
        }
    }
}
